package com.huadao.cart_ccs.experiment.adjustWeight;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseDialogFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.common.ParamsPreferences;
import com.huadao.cart_ccs.common.deviceController.ElectronicScaleController;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class AdjustWeightDF extends BaseDialogFragment implements IAdjustWeightContract.IAdjustWeightView {
    @BindView(R.id.et_real_weight1)
    EditText etRealWeight1;
    @BindView(R.id.et_return_weight1)
    EditText etReturnWeight1;
    @BindView(R.id.et_real_weight2)
    EditText etRealWeight2;
    @BindView(R.id.et_return_weight2)
    EditText etReturnWeight2;
    @BindView(R.id.et_a)
    EditText etA;
    @BindView(R.id.et_b)
    EditText etB;

    private AdjustWeightPresenter adjustWeightPresenter;

    @Override
    protected BasePresenter getPresenter() {
        return adjustWeightPresenter;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialogfrag_adjustweight;
    }

    @Override
    protected void onInit() {
        adjustWeightPresenter=new AdjustWeightPresenter(this);
        String a=String.valueOf(ParamsPreferences.getInstance().getWeightAdjustA());
        String b=String.valueOf(ParamsPreferences.getInstance().getWeightAdjustB());
        etA.setText(a);
        etB.setText(b);
        etA.setSelection(a.length());
        etB.setSelection(b.length());
    }

    @Override
    protected boolean isDialog() {
        return true;
    }

    @Override
    public int getRealWeight1() {
        return Integer.parseInt(etRealWeight1.getText().toString());
    }

    @Override
    public int getRealWeight2() {
        return Integer.parseInt(etRealWeight2.getText().toString());
    }

    @Override
    public void onAdjust(double a, double b) {
        etA.setText(String.valueOf(a));
        etB.setText(String.valueOf(b));
        ElectronicScaleController.getInstance().adjust(a,b);
        showToast("称重校准成功！");
        //记录校准值
        ParamsPreferences.getInstance().setWeightAdjustA(a);
        ParamsPreferences.getInstance().setWeightAdjustB(b);
        ParamsPreferences.getInstance().commit();
        this.dismiss();
    }

    @OnClick({R.id.btn_close,R.id.btn_weight1, R.id.btn_weight2, R.id.btn_adjust})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                this.dismiss();
                break;
            case R.id.btn_weight1:
                adjustWeightPresenter.weight1();
                break;
            case R.id.btn_weight2:
                adjustWeightPresenter.weight2();
                break;
            case R.id.btn_adjust:
                String err=checkParams();
                if(!TextUtils.isEmpty(err)){
                    showDialogMessage("",err);
                    return;
                }
                adjustWeightPresenter.adjust();
                break;
        }
    }

    private String checkParams() {
        String err=null;
        if(TextUtils.isEmpty(etRealWeight1.getText())){
            err="请输入第一个实际重量";
        }else if(TextUtils.isEmpty(etRealWeight2.getText())){
            err="请输入第二个实际重量";
        }else if(TextUtils.isEmpty(etReturnWeight1.getText())){
            err="请称重获取第一个称重重量";
        }else if(TextUtils.isEmpty(etReturnWeight2.getText())){
            err="请称重获取第二个称重重量";
        }
        return err;
    }

    @Override
    public void onWeight1(double returnWeight1) {
        etReturnWeight1.setText(String.valueOf(returnWeight1));
    }

    @Override
    public void onWeight2(double returnWeight2) {
        etReturnWeight2.setText(String.valueOf(returnWeight2));
    }
}
